package net.gyapp.markim.module.contact;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.basic.event.AccountChanged;
import net.gyapp.markim.module.basic.event.AccountIml;
import net.gyapp.markim.module.basic.event.GroupChanged;
import net.gyapp.markim.module.basic.event.GroupIml;
import net.gyapp.markim.utils.RequestParams;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/7/7
 * Version: 1.0
 * Desc:
 */


public class GroupNameActivity extends WrapperSwipeActivity<CommonPresenter> {
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.tv_tip)
    TextView tvTip;

    String text;
    String groupNumber;
    public static Intent getIntent(Context context, String groupNumber, String name) {
        return new Intent(context, GroupNameActivity.class)
                .putExtra("groupNumber", groupNumber)
                .putExtra("name", name);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_modify_text;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("群名称");
        groupNumber = intent.getStringExtra("groupNumber");
        text = intent.getStringExtra("name");
        etText.setText(text);
        etText.setHint("设置群名称");
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if (url.contains(ApiConfig.API_UPDATE_GROUP_NAME)) {
            showToast("修改成功");
            EventBus.getDefault().post(new GroupChanged(GroupIml.SIGN_NAME_CHANGED));
            finish();
        }
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        String text = etText.getText().toString();
        if (TextUtils.isEmpty(text)) {
            showToast("请输入群名称");
            return;
        }

        if (this.text.equals(text)) {
            showToast("请输入要修改的群名称");
            return;
        }

        setGroupName(text);
    }

    private void setGroupName(String groupName) {
        presenter.setNeedDialog(true);
        presenter.postData(ApiConfig.API_UPDATE_GROUP_NAME, new RequestParams().put("groupNumber", groupNumber).put("groupName", groupName).get(), BaseVo.class);
    }
}
